//登录注册模块
let LoginRegist = {

    //***********初始数据定义************/
    page: null,
    app: null,
    api: null,
    path: null,
    hideList: {
        hiddenPhone: true,
        hiddenPwd: true,
        hiddenName: true,
        hiddenPhonecode: true
    },
    alertList: {
        Name: '名字不能为空',
        Phone: '手机号码不能为空',
        Pwd: '请输入6-16位的密码',
        Phonecode: '验证码不能为空'
    },
    Arr: ['Pwd', 'Phonecode', 'Phone'],

    //***********参数获取部分************/
    //传入外部参数
    addParam: function(that, app, api, path) {
        this.page = that;
        this.app = app;
        this.api = api;
        this.path = path;
    },
    //获取输入
    bindInput: function(e) {
        let page = this.page;
        let id = e.currentTarget.id;

        page.setData({
            [id]: e.detail.value
        })
    },
    //失去焦点
    bindblur: function(e) {
        let page = this.page;
        let id = 'hidden' + e.currentTarget.id;

        this.hideList[id] = true
        page.setData({
            [id]: true
        })
    },
    //获得焦点
    bindfocus: function(e) {
        let _this = this;
        let page = this.page;
        let id = 'hidden' + e.currentTarget.id;

        _this.hideList[id] = false;
        page.setData({
            "hiddenPhone": _this.hideList["hiddenPhone"],
            "hiddenPwd": _this.hideList["hiddenPwd"],
            "hiddenName": _this.hideList["hiddenName"],
            "hiddenPhonecode": _this.hideList["hiddenPhonecode"]
        })
    },
    //***********登录注册部分************/
    //注册
    regist: function(e, suc) {
        let _this = this;
        let page = this.page;
        let app = this.app;
        let api = this.api;

        if (!_this.checkEmpty('Phone', 'Phonecode', 'Pwd')) return;

        let dataDic = {
            phone: page.data.Phone,
            Code: page.data.Phonecode,
            UserName: page.data.Name,
            UserPwd: page.data.Pwd,
            Source: app.globalData.appSource,
            SourceType: 2,
            OpenId: app.globalData.openId,
        };

        wx.showLoading({
            title: "加载中",
        })

        api.methodWechatRegister({
            dataDic,
            complete: function(res) {
                console.log(res);
                wx.hideLoading();
                if (res.data.code != 0) {
                    wx.showModal({
                        title: '温馨提示',
                        showCancel: false,
                        content: res.data.msg,
                    })
                    return;
                } else {
                    typeof suc == 'function' && suc();
                    _this.nextLogin();
                    setTimeout(() => {
                        wx.showModal({
                            title: '注册成功!',
                            content: '我们的课程顾问将会通过电话联系您进行预约上课，请耐心等待',
                            showCancel: false,
                            success: function(res) {
                                if (res.confirm) {
                                    console.log('用户点击确定')
                                } else if (res.cancel) {
                                    console.log('用户点击取消')
                                }
                            }
                        })
                    }, 2000);
                }
            }
        })
    },
    //登录
    nextLogin: function(suc) {
        let _this = this;
        let page = this.page;
        let app = this.app;
        let api = this.api;
        let path = this.path;

        let dataDic = {
            UserPwd: page.data.Pwd,
            UserAccount: page.data.Phone,
            OpenId: app.globalData.openId,
        }

        api.methodWechatLogin({
            dataDic,
            success: (res) => {
                console.log(res);
                wx.hideLoading();
                if (res.data.code != 0) {
                    wx.showModal({
                        title: '温馨提示',
                        showCancel: false,
                        content: res.data.msg,
                    })
                    return;
                } else {
                    //登录成功后，同步用户数据，返回个人中心界面
                    app.globalData.loginStatus = true;
                    wx.setStorage({
                        key: 'loginStatus',
                        data: true,
                    })
                    typeof suc == 'function' && suc();
                    if (page.data.fromPage == "score") {
                        console.log(getCurrentPages())
                        let pagesArr = getCurrentPages()
                        for (let i = 0; i < pagesArr.length; i++) {
                            let pageItem = pagesArr[i]
                            if (pageItem.route == path.scorePreview) {
                                wx.navigateBack({
                                    delta: pagesArr.length - i - 1
                                })
                                return
                            }
                        }
                    } else {
                        wx.switchTab({
                            url: '/' + path.index
                        })
                    }
                }
            },
            fail: (res) => {
                console.log(res);
                wx.showModal({
                    title: '温馨提示',
                    showCancel: false,
                    content: '自动登录失败，请返回登录界面重新登录！',
                    success: function(res) {
                        if (res.confirm) {
                            wx.redirectTo({
                                url: '/' + path.login + '?fromPage=' + page.data.fromPage,
                            })
                        }
                    }
                });
            }
        });
    },
    //账号密码登录
    login: function(suc) {
        let _this = this;
        let page = this.page;
        let app = this.app;
        let api = this.api;
        let path = this.path;

        if (!_this.checkEmpty('Phone', '', 'Pwd')) return;

        wx.showLoading({
            title: '登录中...',
        })

        let dataDic = {
            UserPwd: page.data.Pwd,
            UserAccount: page.data.Phone,
            OpenId: app.globalData.openId,
        };

        api.methodWechatLogin({
            dataDic,
            success: (res) => {
                console.log(res);
                wx.hideLoading();
                if (res.data.code != 0) {
                    if (res.data.code == -6) {
                        console.log("密码错误")
                        wx.showModal({
                            title: '温馨提示',
                            content: res.data.msg,
                            showCancel: false,
                            success: function(res) {
                                if (res.confirm) {
                                    console.log("点击确定")
                                }
                            }
                        })
                    } else if (res.data.code == -4) {
                        console.log("未注册")
                        wx.showModal({
                            title: '温馨提示',
                            showCancel: true,
                            confirmText: "去注册",
                            content: res.data.msg,
                            success: function(res) {
                                console.log("++=")
                                if (res.confirm) {
                                    wx.navigateTo({
                                        url: '/' + path.regist
                                    })
                                } else if (res.cancel) {
                                    console.log("取消")
                                }
                            }
                        })
                    }
                    return;
                } else {
                    console.log('登录成功返回的数据', res.data);
                    app.globalData.UID = res.data.data.UID;
                    //登录成功后，同步用户数据，返回个人中心界面
                    app.globalData.loginStatus = true;
                    wx.setStorage({
                        key: 'loginStatus',
                        data: true,
                    })
                    wx.setStorage({
                        key: 'UID',
                        data: res.data.data.UID,
                    })
                    typeof suc == 'function' && suc();
                    if (page.data.fromPage == "index") {
                        console.log("这个是界面的数据", getCurrentPages())
                        let pagesArr = getCurrentPages()
                        for (let i = 0; i < pagesArr.length; i++) {
                            let pageItem = pagesArr[i]
                            if (pageItem.route == path.index) {
                                console.log(pagesArr.length, i)
                                wx.navigateBack({
                                    delta: pagesArr.length - i - 1
                                })
                                return
                            }
                        }
                    } else {
                        wx.switchTab({
                            url: '/' + path.my
                        })
                    }
                }
            },
            fail: (res) => {
                console.log(res);
                wx.showModal({
                    title: '温馨提示',
                    showCancel: false,
                    content: '登录失败，请检查网络情况！',
                });
            }
        });
    },
    //短信验证码登录
    login_Code: function(suc) {
        let _this = this;
        let page = this.page;
        let app = this.app;
        let api = this.api;
        let path = this.path;

        if (!_this.checkEmpty('Phone', 'Phonecode', '')) return;

        wx.showLoading({
            title: '登录中...',
        })

        let dataDic = {
            TelePhone: page.data.Phone,
            Code: page.data.Phonecode,
            //OpenId: app.globalData.openId,
        };
        api.methodWechatLoginByVerificationCode({
            dataDic,
            success: (res) => {
                console.log(res);
                wx.hideLoading();
                if (res.data.code != 0) {
                    wx.showModal({
                        title: '温馨提示',
                        showCancel: false,
                        content: res.data.msg,
                    })
                    return;
                } else {
                    console.log('登录成功返回的数据', res.data);
                    app.globalData.UID = res.data.data.UID;
                    //登录成功后，同步用户数据，返回个人中心界面
                    app.globalData.loginStatus = true;
                    wx.setStorage({
                        key: 'loginStatus',
                        data: true,
                    })
                    wx.setStorage({
                        key: 'UID',
                        data: res.data.data.UID,
                    })
                    typeof suc == 'function' && suc();
                    if (page.data.fromPage == "index") {
                        console.log("这个是界面的数据", getCurrentPages())
                        let pagesArr = getCurrentPages()
                        for (let i = 0; i < pagesArr.length; i++) {
                            let pageItem = pagesArr[i]
                            if (pageItem.route == path.index) {
                                console.log(pagesArr.length, i)
                                wx.navigateBack({
                                    delta: pagesArr.length - i - 1
                                })
                                return
                            }
                        }
                    } else {
                        wx.switchTab({
                            url: '/' + path.my
                        })
                    }
                }
            },
            fail: (res) => {
                console.log(res);
                wx.showModal({
                    title: '温馨提示',
                    showCancel: false,
                    content: '登录失败，请检查网络情况！',
                });
            }
        })
    },
    //验证数据是否为空
    checkEmpty: function(a, b, c) {
        let _this = this;
        let page = this.page;
        let flag = true

        if (a == 'Phone' && page.data['Phone'].length <= 0) {
            wx.showModal({
                title: '温馨提示',
                showCancel: false,
                content: _this.alertList.Phone
            })
            flag = false;
            return false;
        }
        if (b == 'Phonecode' && page.data['Phonecode'].length <= 0) {
            wx.showModal({
                title: '温馨提示',
                showCancel: false,
                content: _this.alertList.Phonecode
            })
            flag = false;
            return false;
        }
        if (c == 'Pwd' && page.data['Pwd'].length <= 5 || page.data['Pwd'].length > 16) {
            wx.showModal({
                title: '温馨提示',
                showCancel: false,
                content: _this.alertList.Pwd
            })
            flag = false;
            return false;
        }
        return flag
    },

    //***********验证码部分************/
    //点击获取短信验证码
    tapGetAuthCode: function(e, category) {
        let _this = this;
        let page = this.page;

        if (page.data.Phone.length > 0) {
            if (category != 'login') {
                _this.getAuthCode();
            } else {
                _this.getAuthCode_login();
            }
        } else {
            wx.showModal({
                title: '温馨提示',
                showCancel: false,
                content: '请填写正确的手机号',
            });
            return false;
        }
    },
    //注册获取验证码
    getAuthCode: function() {
        let _this = this;
        let page = this.page;
        let app = this.app;
        let api = this.api;
        let path = this.path;

        let dataDic = {
            VerifyType: 0,
            phone: page.data.Phone,
            Type: 1,
            SMSType: 0,
        };
        api.methodWechatGetSMSCode({
            dataDic,
            success: (res) => {
                console.log(res);
                page.setData({
                    verification: res.data.data.code
                })
                if (page.data.verification == -9) {
                    console.log("不调用60秒倒计时函数")
                } else if (page.data.verification == 0) {
                    _this.countdownTime();
                }
                console.log(page.data.verification)
                if (res.data.code != 0) {
                    wx.showModal({
                        title: '温馨提示',
                        showCancel: false,
                        content: res.data.msg,
                    });
                    return;
                } else {
                    if (res.data.data.code != 0) {
                        wx.showModal({
                            title: '温馨提示',
                            showCancel: false,
                            content: res.data.data.msg,
                        })
                    } else {
                        wx.showToast({
                            title: '发送成功',
                        })
                    }
                }
            },
            fail: (res) => {
                console.log(res);
                wx.showModal({
                    title: "温馨提示",
                    showCancel: false,
                    content: "获取验证码失败，请稍后重试！",
                });
            }
        });
    },
    //登录获取验证码
    getAuthCode_login: function() {
        let _this = this;
        let page = this.page;
        let app = this.app;
        let api = this.api;
        let path = this.path;

        let dataDic = {
            // VerifyType: 0,
            TelePhone: page.data.Phone,
            // Type: 1,
            // SMSType: 0,
        };
        api.methodWechatGetVerificationCode({
            dataDic,
            success: (res) => {
                console.log(res);
                page.setData({
                    verification: res.data.code
                })
                if (page.data.verification == -4) {
                    console.log("+++")
                } else if (page.data.verification == 0) {
                    console.log("----")
                    _this.countdownTime();
                }
                console.log(page.data.verification)
                console.log(res.data.code)
                if (res.data.code != 0) {
                    if (res.data.code == -7) {
                        wx.showModal({
                            title: '温馨提示',
                            showCancel: false,
                            content: res.data.msg,
                            success: function(res) {
                                console.log(res)
                                if (res.confirm) {
                                    console.log('用户点击确定')
                                }
                            }
                        })
                    } else if (res.data.code == -4) {
                        wx.showModal({
                            title: '温馨提示',
                            showCancel: true,
                            confirmText: "去注册",
                            content: res.data.msg,
                            success: function(res) {
                                console.log("测试弹框")
                                console.log(res)
                                if (res.confirm) {
                                    wx.navigateTo({
                                        url: '/' + path.regist
                                    })
                                    console.log('用户点击确定')
                                } else if (res.cancel) {
                                    console.log('用户点击取消')
                                }
                            }
                        });
                    }
                    return;
                } else {
                    if (res.data.code != 0) {
                        wx.showModal({
                            title: '温馨提示',
                            showCancel: true,
                            content: res.data.msg,
                            success: function(res) {
                                console.log(res)
                                if (res.confirm) {
                                    console.log('用户点击确定')
                                } else if (res.cancel) {
                                    console.log('用户点击取消')
                                }
                            }
                        })
                    } else {
                        wx.showToast({
                            title: '发送成功',
                        })
                    }
                }
            },
            fail: (res) => {
                console.log(res);
                wx.showModal({
                    title: "温馨提示",
                    showCancel: false,
                    content: "获取验证码失败，请稍后重试！",
                });
            }
        });
    },
    //倒计时
    countdownTime: function() {
        let _this = this;
        let page = this.page;
        let initTime = 60;

        page.setData({
            getCode_ing: true,
            remainTime: initTime
        })

        page.downTimer = setInterval(function() {
            initTime--;
            page.setData({
                remainTime: initTime
            })
            if (initTime <= 0) {
                clearInterval(page.downTimer);
                page.setData({
                    getCode_ing: false
                })
            }
        }, 1000);

    },
    showThis: function(params) {
        console.log('showThis', this);
        return true
    }
}

module.exports = LoginRegist